<?php

namespace Modules\Wechat\Admin;

use Duxravel\Core\Events\ManageForm;
use Duxravel\Core\UI\Form;
use Duxravel\Core\UI\Table;
use Illuminate\Support\Facades\Log;
use Modules\System\Admin\Expend;

class Setting extends Expend
{

    public string $model = \Modules\Wechat\Model\Setting::class;

    protected function table()
    {
        $table = new Table(new $this->model());
        $table->model()->orderBy("id","desc");
        //filter
        $table->filter('配置名称', 'name')->text('请输入配置名称搜索')->condition("like")->quick();
        //action
        $table->action()->button('添加', 'admin.wechat.setting.page')->type('dialog')->icon('plus');
        //column
        $table->column("id","id")->sorter();
        $table->column("配置名称","name");

        $column = $table->column('操作');
        $column->link('编辑', 'admin.wechat.setting.page', ['id' => 'id'])->type('dialog')->icon('edit');
        $column->link('删除', 'admin.wechat.setting.del', ['id' => 'id'])
            ->type('ajax', ['method' => 'post'])
            ->icon('delete')
            ->class("arco-link-status-danger")->attr('vIf','rowData.record.id!=1');
        return $table;
    }

    public function form(int $id = 0): Form
    {
        $form = new Form(new $this->model());
        $form->title("添加微信配置");
        $form->setKey('id', $id);
        $form->text('配置名称', 'name')->must();
        $tab = $form->tab();
        $row = $form->info();
        $tab->column('小程序配置',function (Form $form) use ($row) {
            $values = ['app_id'=>'','secret'=>'','token'=>'','aes_key'=>''];
            if($row){
                $values = $row->getAttribute('mini_app');
            }
            $form->text('小程序appid', 'mini_app_app_id')->value($values['app_id']);
            $form->text('小程序secret', 'mini_app_secret')->value($values['secret']);
            $form->text('小程序token', 'mini_app_token')->value($values['aes_key']);
            $form->text('小程序aes_key', 'mini_app_aes_key')->value($values['app_id'])->prompt('EncodingAESKey，兼容与安全模式下请一定要填写！！！');
        });
        $tab->column('公众号配置',function (Form $form) use ($row) {
            $values = ['app_id'=>'','secret'=>'','token'=>'','aes_key'=>''];
            if($row){
                $values = $row->getAttribute('official_account');
            }
            $form->text('公众号appid', 'official_account_app_id')->value($values['app_id']);
            $form->text('公众号secret', 'official_account_secret')->value($values['secret']);
            $form->text('公众号token', 'official_account_token')->value($values['token']);
            $form->text('公众号aes_key', 'official_account_aes_key')->value($values['aes_key'])->prompt('EncodingAESKey，兼容与安全模式下请一定要填写！！！');
        });
        $tab->column('支付配置',function (Form $form) use ($row) {
            $values = ['mch_id'=>'','key'=>'','key_path'=>'','cert_path'=>''];
            if($row){
                $values = $row->getAttribute('payment');
            }
            $form->text('商户号', 'payment_mch_id')->value($values['mch_id']);
            $form->text('支付密钥', 'payment_key')->value($values['key'])->help("32位密钥",true);
            $form->textarea('证书apiclientKey内容', 'payment_key_path')->value($values['key_path']);
            $form->textarea('证书apiclientCert内容', 'payment_cert_path')->value($values['cert_path']);
        });
        $tab->column('开放平台配置',function (Form $form) use ($row) {

        });
        $tab->column('企业微信配置',function (Form $form) use ($row) {

        });
        $form->before(function($data, $time, $model) {
            $mini_app = [];
            $official_account = [];
            $payment = [];
            foreach ($data as $key=>$value){
                if(str_contains($key,'mini_app')){
                    $mini_app[str_replace('mini_app_','',$key)] = $value?:'';
                }elseif(str_contains($key,'official_account')){
                    $official_account[str_replace('official_account_','',$key)] = $value?:'';
                }elseif(str_contains($key,'payment')){
                    $payment[str_replace('payment_','',$key)] = $value?:'';
                }
            }
            $model->mini_app = $mini_app;
            $model->official_account = $official_account;
            $model->payment = $payment;
        });
        return $form;
    }

}
